package com.xuxin.baby.utils;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.widget.DatePicker;
import android.widget.TimePicker;

import com.blankj.utilcode.constant.TimeConstants;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.xuxin.baby.R;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

/**
 * Created by xu_xin
 * Created on 2020.08.11 0001
 * 日期选择器
 */
public class DatePickerUtils {

    public interface onListener {
        void onShow(String time);
    }

    /**
     * 年月日
     */
    public static void showDate(Context context, onListener listener) {
        Calendar calendar = Calendar.getInstance();
        new DatePickerDialog(context, R.style.DatePicker, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker datePicker, int year, int month, int dayOfMonth) {
                month++;
                String mMonth = month >= 10 ? String.valueOf(month) : "0" + month;
                String mDay = dayOfMonth >= 10 ? String.valueOf(dayOfMonth) : "0" + dayOfMonth;
                String mData = year + "-" + mMonth + "-" + mDay;
                SimpleDateFormat format = TimeUtils.getSafeDateFormat("yyyy-MM-dd");
                long span = TimeUtils.getTimeSpan(TimeUtils.getNowString(format), mData, format, TimeConstants.SEC);
                if (span >= 0) {
                    listener.onShow(mData);
                } else {
                    ToastUtils.showShort("不能超过当前时间");
                }
            }
        }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();
    }

    /**
     * 时分秒
     */
    public static void showTime(Context context, onListener listener) {
        Calendar calendar = Calendar.getInstance();
        new TimePickerDialog(context, R.style.DatePicker, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker timePicker, int hourOfDay, int minute) {
                String mHour = hourOfDay >= 10 ? String.valueOf(hourOfDay) : "0" + hourOfDay;
                String mMinute = minute >= 10 ? String.valueOf(minute) : "0" + minute;
                String mTime = mHour + ":" + mMinute + ":00";
                listener.onShow(mTime);
            }
        }, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), true).show();
    }

    /**
     * 年月日时分秒
     */
    public static void showDateTime(Context context, onListener listener) {
        Calendar calendar = Calendar.getInstance();
        new DatePickerDialog(context, R.style.DatePicker, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker datePicker, int year, int month, int dayOfMonth) {
                month++;
                String mMonth = month >= 10 ? String.valueOf(month) : "0" + month;
                String mDay = dayOfMonth >= 10 ? String.valueOf(dayOfMonth) : "0" + dayOfMonth;
                String mData = year + "-" + mMonth + "-" + mDay;
                showTime(context, new onListener() {
                    @Override
                    public void onShow(String time) {
                        String mTime = mData + " " + time;
                        SimpleDateFormat format = TimeUtils.getSafeDateFormat("yyyy-MM-dd HH:mm:ss");
                        long span = TimeUtils.getTimeSpan(TimeUtils.getNowString(format), mTime, format, TimeConstants.SEC);
                        if (span >= 0) {
                            listener.onShow(mTime);
                        } else {
                            ToastUtils.showShort("不能超过当前时间");
                        }
                    }
                });
            }
        }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();
    }

    /**
     * 今日时分秒
     */
    public static void showTodayTime(Context context, onListener listener) {
        Calendar calendar = Calendar.getInstance();
        new TimePickerDialog(context, R.style.DatePicker, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker timePicker, int hourOfDay, int minute) {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINESE);
                String mHour = hourOfDay >= 10 ? String.valueOf(hourOfDay) : "0" + hourOfDay;
                String mMinute = minute >= 10 ? String.valueOf(minute) : ("0" + minute);
                String mTime = TimeUtils.getNowString(format) + " " + mHour + ":" + mMinute + ":00";
                SimpleDateFormat format2 = TimeUtils.getSafeDateFormat("yyyy-MM-dd HH:mm:ss");
                long span = TimeUtils.getTimeSpan(TimeUtils.getNowString(format2), mTime, format2, TimeConstants.SEC);
                if (span >= 0) {
                    listener.onShow(mTime);
                } else {
                    ToastUtils.showShort("不能超过当前时间");
                }
            }
        }, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), true).show();
    }
}